$(document).ready(function () {
    $("#myModal").modal("hide");//绑定模态框的id，加载页面时先隐藏
    $.ajax({
        type: "get",
        url: SERVER_URL+"/appointment/detail/"+sessionStorage.getItem("userEmail"),
        async: true,
        dataType: "json",
        success: function (data) {
            let str = '<tr>';
            for(i=0;i<data.data.length;i++){
                str += '<td>'+data.data[i].office+'</td><td>'+data.data[i].doctorName+'</td><td>'+data.data[i].dateTime+'</td><td>'+
                '<a onclick="cancelAppointment('+data.data[i].appointmentId+')" class="btn btn-danger btn-icon-split">'+
                '<span class="icon text-white-50"><i class="fas fa-trash"></i></span>'+
                '<span class="text" style="color: white">Cancel&nbsp;</span></a></td>';
            }
            str += '</tr>';
            $("#appointmentResult").html(str)
        }
    });
})


function searchSymptoms() {
    let searchInput = document.getElementById("searchText");
    $.ajax({
        type: "GET",
        url: SERVER_URL+"/department-dtl/symptoms/"+searchInput.value,
        async: true,
        dataType: "json",
        success: function (data) {
            let str = ''
            if (data.data.length === 0){
                str = "no office"
            }else {
                //TODO: 将Id换成数据
                str +='<table class="table table-hover"><caption>Suggestion</caption>'+
                    '<thead class="thead-light"><tr><th scope="col">#</th><th scope="col">Department</th><th scope="col">Office</th><th scope="col">Doctor</th><th scope="col">Action</th></tr></thead><tbody>';

                for(let i=0;i<data.data.length;i++){
                    str += '<tr><td>'+(i+1)+'</th><td>'+data.data[i].departmentId+'</td><td id="office'+i+'">'+data.data[i].departmentDtlName+'</td><td>'+data.data[i].status+'</td>'+
                        '<td><button data-toggle="modal" data-target="#myModal" onclick="setValue('+i+')" class="btn btn-primary btn-icon-split"><span class="icon text-white-50"><i class="fas fa-flag"></i></span><span class="text text-light">Appointment</span></button ></td></tr>';
                }
                str += '</tbody></table>';
                $("#suggestDepartment").html(str)
            }
        }
    });
}

function makeAppointment() {
    let doctor = document.getElementById("appointmentDoctor");
    let dateTime = document.getElementById("dateTimeText");

    //console.log(SERVER_URL+"/appointment/"+sessionStorage.getItem("userEmail")+"/"+doctor.value+"/"+dateTime.value);
    $.ajax({
        type: "post",
        url: SERVER_URL+"/appointment/"+sessionStorage.getItem("userEmail")+"/"+doctor.value+"/"+dateTime.value,
        async: true,
        dataType: "json",
        success: function (data) {
            window.location.href="PSymptoms.html";
        }
    });
}

function setValue(i) {
    let office = document.getElementById("office"+i)
    $("#appointmentOffice").val(office.innerText);
    $("#appointmentDoctor").val('allwayzio@126.com');
}

function cancelAppointment(Id) {
    $.ajax({
        type: "delete",
        url: SERVER_URL+"/appointment/"+Id,
        async: true,
        dataType: "json",
        success: function (data) {
            window.location.href="PSymptoms.html";
        }
    });
}